import com.sun.j3d.loaders.Loader;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.media.j3d.*;
import javax.vecmath.*;


public class UstawieniaSceny
{
    private SimpleUniverse universe;
    private ViewingPlatform vp;
    private BranchGroup group;

    private Canvas3D canvas;
    private Bron[] bronie = new Bron[3];
    private TransformGroup[] modele = new TransformGroup[3];

    
    public UstawieniaSceny() throws IOException
    {   
        initCanvas();
        initUniverse();
        
        vp = universe.getViewingPlatform();
        
        BoundingSphere bounds = new BoundingSphere();
        dodajModele(bounds);
        dodajSwiatlo(bounds);
        dodajTlo(bounds);
        
        group.compile();
        universe.addBranchGraph(group); 
    }
    
    
    private void initUniverse()
    {
        universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();
    }
    
    private void initCanvas()
    {
        canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        canvas.setDoubleBufferEnable(true);
        canvas.setFocusable(true);
    }
    
    private void dodajModele(BoundingSphere b) throws IOException
    {
    	group = new BranchGroup();
    	
    	for(int i=0; i<3; i++)
    	{
    		bronie[i] = new Bron(i+1, b);
    		modele[i] = bronie[i].getModel();
    		group.addChild(modele[i]);

    		bronie[i].zmienWidocznosc();
    	}
    	bronie[0].zmienWidocznosc();

    }
    
    private void dodajSwiatlo(BoundingSphere b)
    {
        Color3f lightColor = new Color3f(Color.BLUE);
        Vector3f lightDirection = new Vector3f(4.0f, -7.0f, -12.0f);
        DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(b);
        group.addChild(light);

    }
    
    private void dodajTlo(BoundingSphere b)
    {
        Background background = new Background(new Color3f(Color.LIGHT_GRAY)); 
        background.setApplicationBounds(b); 
        group.addChild(background); 

    }
    
    public void zmienWidocznoscBroni(int index)
    {
    	bronie[index].zmienWidocznosc();
    }
    
    public Canvas3D getCanvas()
    {
        return canvas;
    }
    
    public Bron[] getTablicaBroni()
    {
    	return this.bronie;
    }
    
    public ViewingPlatform getViewingPlatform()
    {
    	return this.vp;
    }
    
    public BranchGroup getBranchGroup()
    {
    	return this.group;
    }
    
    
    public SimpleUniverse getUniverse()
    {
    	return this.universe;
    }
}
